package com.shntec.temp.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.shntec.bp.json.JSONArray;
import com.shntec.bp.json.JSONException;
import com.shntec.bp.json.JSONObject;

import com.shntec.bp.common.ShntecBaseAction;
import com.shntec.bp.common.ShntecErrorCode;
import com.shntec.bp.common.JSONMessageConstant;
import com.shntec.bp.common.RequestMessageParser;
import com.shntec.bp.common.ResponseMessageGenerator;
import com.shntec.bp.util.DatabaseManager;
import com.shntec.bp.util.ShntecLogger;
import com.shntec.temp.util.MySQLConn;

public class GetActionItemListAction extends ShntecBaseAction {

	public GetActionItemListAction () {
		super.actionCode = 20010;
		super.actionName = "GetActionItemListAction";
		super.actionDescription = "获取全部的action";
		// No need authentication
		super.needAuthenticationCheck = false;
	}

	@Override
	public ResponseMessageGenerator handleAction(RequestMessageParser parser) {

		ResponseMessageGenerator generator = new ResponseMessageGenerator();

		JSONObject payload = null;
		JSONObject parameter = parser.getParameterJsonObject();
		
		
		if (parameter == null) {

			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER);
		}
		
		
		MySQLConn mysql = new MySQLConn();
		Connection conn = null;
		payload = new JSONObject();
		PreparedStatement pstmt = null;
		JSONArray actions = new JSONArray();
		ResultSet rs = null;
		try {
			conn = mysql.getConn();
			pstmt = conn.prepareStatement("select * from temp_actions order by actionCode asc");
		
			
			rs = pstmt.executeQuery();
			
			
			while(rs.next()){
				JSONObject actionItem = new JSONObject();
				
				actionItem.put("aid", rs.getInt("aid"));
				actionItem.put("actionName", rs.getString("actionName"));
				actionItem.put("actionCode", rs.getString("actionCode"));
				actionItem.put("actionDesc", rs.getString("actionDesc"));
				actionItem.put("actionRequest", rs.getString("actionRequest"));
				actionItem.put("actionSuccessPayload", rs.getString("actionSuccessPayload"));
				actionItem.put("actionErrorPayload", rs.getString("actionErrorPayload"));
				
				actions.put(actionItem);
				
	
			}
			
			payload.put("actions", actions);
			
			return generator.toSuccess(parser, payload);
			
			
		} catch (ClassNotFoundException e) {
			ShntecLogger.logger.error("ClassNotFoundException" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"ClassNotFoundException");
		} catch (SQLException e) {
			ShntecLogger.logger.error("SQLException");
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"SQLException");
		} catch (JSONException e) {
			ShntecLogger.logger.error("JSONException");
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"JSONException");
		}finally{
			if(conn != null){
				
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			if(pstmt != null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
	}

}
